﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aoxuelf.MDSTool.Model;
namespace Aoxuelf.MDSTool.SQL
{
    public class BLLQueryMethodWriter : MethodWriter
    {
        public BLLQueryMethodWriter(BLLQueryMethodInfo methodWriteInfo)
            : base(methodWriteInfo)
        {
        }

        private bool IsPage
        {
            get { return ((BLLQueryMethodInfo)this.methodWriteInfo).IsPage; }
        }

        protected override void WriteSQLParams()
        {
            base.WriteSQLParams();
            if (IsPage)
            {
                WriteSQLParam(SQLParamInfo.PageSize);
                WriteSQLParam(SQLParamInfo.PageNO);
            }
        }

        protected override void WriteMethodSig()
        {
            string format = "public IDictionary<string, object> {0}(IDictionary<string, string> dicParam)";
            builder.AppendLine(string.Format(format, methodWriteInfo.MethodName));
        }

        protected override void WriteMethodBody()
        {
            builder.AppendLine("IDictionary<string, object> dicRet = new Dictionary<string, object>();");
            builder.AppendLine("");
            builder.AppendLine("try");
            builder.AppendLine("{");
            if (IsPage)
            {
                builder.AppendLine(string.Format("dicRet = dal.{0}(dicParam);", methodWriteInfo.MethodName));
            }
            else
            {
                builder.AppendLine(string.Format("DataTable dt = dal.{0}(dicParam);", methodWriteInfo.MethodName));
                builder.AppendLine("");
                builder.AppendLine("dicRet = ExecutionResult.QuerySuccess();");  
                builder.AppendLine("dicRet[\"DT\"] = dt;");                              
            }            
            builder.AppendLine("}");
            builder.AppendLine("catch (Exception ex)");
            builder.AppendLine("{");
            builder.AppendLine("    dicRet = ExceptionHelper.ExceptioinManage(ex, dicRet);");
            builder.AppendLine("    return dicRet;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("return dicRet;");
        }

        protected override void WriteReturnBody()
        {
            if (IsPage)
            {
                builder.AppendLine("/// <content type=\"DataTable\" name=\"#DT_RESULT#\">");
                base.WriteColumns();
                builder.AppendLine("/// </content>");
                WriteContent(ReturnContent.Page_TotalCount);
                WriteContent(ReturnContent.Page_TotalPage);
                WriteContent(ReturnContent.Exe_Flag);
                WriteContent(ReturnContent.Exe_UserMsg);
            }
            else
            {
                base.WriteReturnBody();
                WriteContent(ReturnContent.Exe_Flag);
                WriteContent(ReturnContent.Exe_UserMsg);
            }            
        }
    }
}
